//
//  Dialect.m
//  DialectShare
//
//  Created by timmy zhong on 3/20/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "Dialect.h"
#import "UserDB.h"
#import "Const.h"

@implementation Dialect
@synthesize dialectId = _dialectId, sentenceId = _sentenceId, yuxi = _yuxi, yuxiId = _yuxiId, content = _content, address = _address, uri = _uri, dateline = _dateline, good = _good, bad = _bad, listen = _listen, longitude = _longitude, latitude = _latitude, distance = _distance, uid = _uid, user = _user, object = _object;

- (void)dealloc
{
    self.yuxi = nil;
    self.content = nil;
    self.address = nil;
    self.uri = nil;
    self.distance = nil;
    self.object = nil;
    
    [super dealloc];
}
- (User *)user
{
    UserDB *db = [[UserDB alloc] init];
    NSString *condition = [NSString stringWithFormat:@"%@=%d", UserUid, self.uid];
    NSArray *users = [db condition:condition start:0 count:2];
    if ([users count] == 0) {
        _user = [[User alloc] init];
    } else {
        _user = [[users objectAtIndex:0] retain];
    }
    [db release];

    return _user;
}


+ (Dialect *)dialectWithDic:(NSDictionary *)dic
{
    Dialect *dialect = [[Dialect alloc] init];
    dialect.dialectId = [[dic objectForKey:@"id"] intValue];
    dialect.uid = [[dic objectForKey:@"uid"] intValue];
    dialect.sentenceId = [[dic objectForKey:@"sentence_id"] intValue];
    dialect.longitude = [[dic objectForKey:@"lng"] floatValue];
    dialect.latitude = [[dic objectForKey:@"lat"] floatValue];
    dialect.address = [dic objectForKey:@"address"];
    dialect.uri = [dic objectForKey:@"uri"];
    dialect.dateline = [[dic objectForKey:@"dateline"] intValue];
    dialect.good = [[dic objectForKey:@"good"] intValue];
    dialect.bad = [[dic objectForKey:@"bad"] intValue];
    dialect.listen = [[dic objectForKey:@"listen"] intValue];
    dialect.yuxiId = [[dic objectForKey:@"yuxi_id"] intValue];
    dialect.distance = [dic objectForKey:@"di"];
    dialect.yuxi = [dic objectForKey:@"yuxi"];
    dialect.content = [dic objectForKey:@"content"];
    return [dialect autorelease];
}

@end
